package com.code.translate.plugins;

import com.code.translate.enums.Constants;
import com.code.translate.function.ImageTranslatePlugin;
import com.code.translate.utils.HttpUtils;
import com.code.translate.utils.JsonpathUtils;
import com.code.translate.utils.StringUtils;
import org.springframework.http.HttpMethod;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ggh
 * @date 2022/8/4
 * <p>
 * 科大讯飞翻译
 */
public class XunFeiTranslatePlugin extends ImageTranslatePlugin {
    private final static String indexUrl = "https://fanyi.xfyun.cn/console/trans/text";
    private final static String translateUrl = "https://fanyi.xfyun.cn/api-tran/trans/its";
    private final static String refer = "https://fanyi.xfyun.cn/console/trans/text";

    private final static String UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36";
    private final static Integer signleTranslateTextLen = 2000;

    // 图片翻译
    private final static String translateImageUrl = "https://fanyi.xfyun.cn/api-tran/img/exec";

    @Override
    protected Map<String, Object> prepareImage() throws Exception {
        return null;
    }

    @Override
    protected String doTranslateImage(File text, Map<String, Object> params) throws Exception {
        return null;
    }

    @Override
    protected Map<String, Object> prepare() {
        return null;
    }

    @Override
    protected String doTranslate(String text, Map<String, Object> params) throws Exception {
        String response;
        Map<String, String> headers = new HashMap<>(3);
        headers.put("User-Agent", UserAgent);
        headers.put("Referer", refer);

        if (text.length() > signleTranslateTextLen) {
            List<String> singleTextList = StringUtils.splitString(text, signleTranslateTextLen, Constants.SEGEMENT);

            Map<String, Object> requestParams = structCommonParams();

            StringBuffer buffer = new StringBuffer();
            for (String singleText : singleTextList) {
                loadTextParams(requestParams, singleText);

                response = HttpUtils.req(translateUrl, HttpMethod.POST, headers, requestParams);
                buffer.append(doExtract(response));
            }
            return buffer.toString();
        } else {
            Map<String, Object> requestParams = structParams(text);

            response = HttpUtils.req(translateUrl, HttpMethod.POST, headers, requestParams);

            return doExtract(response);
        }
    }

    @Override
    protected String doExtract(String response) {
        if (response.contains("\"code\":0")) {
            String tempJson = JsonpathUtils.read(response, "$.data");
            if (!StringUtils.isEmpty(tempJson)) {
                return JsonpathUtils.read(tempJson, "$.trans_result.dst");
            }
        } else {
            System.out.println(response);
            return null;
        }
        return null;
    }

    private static Map<String, Object> structParams(String text) throws Exception {
        Map<String, Object> params = new HashMap<>(3);
        params.put("from", "en");
        params.put("to", "cn");
        params.put("text", text);

        return params;
    }

    private static Map<String, Object> structCommonParams() {
        Map<String, Object> params = new HashMap<>(3);
        params.put("from", "en");
        params.put("to", "cn");

        return params;
    }

    private static Map<String, Object> loadTextParams(Map<String, Object> params, String text) throws Exception {
        params.put("text", text);

        return params;
    }
}
